﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Media.Imaging;

namespace BabySmash7
{
    public partial class MainPage : PhoneApplicationPage
    {
        bool sound = true;

        public MainPage()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            Shapes.CoolSquare userControl = new Shapes.CoolSquare(Utils.GetGradientBrush(Colors.Purple));
            userControl.Width = 100;
            userControl.Height = 100;
            Canvas.SetLeft(userControl, Convert.ToInt32((this.ActualWidth / 2) - userControl.Width));
            Canvas.SetTop(userControl, Convert.ToInt32((this.ActualHeight / 2) - userControl.Height));
            figuresCanvas.Children.Add(userControl);
        }

        private void figuresCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            panelStart.Visibility = Visibility.Collapsed;

            addRandomUserControl();
            playRandomSound();

            if (figuresCanvas.Children.Count > 6)
            {
                figuresCanvas.Children.RemoveAt(0);
            }
        }

        private void addRandomUserControl()
        {
            int randomNumber = Utils.RandomBetweenTwoNumbers(1, 4);

            switch (randomNumber)
            {
                case 1:
                    addRandomLetter();
                    break;
                case 2:
                    addRandomNumber();
                    break;
                case 3:
                    addRandomSymbol();
                    break;
                case 4:
                    addRandomSymbol();
                    break;
            }
        }

        private void addRandomLetter()
        {
            UserControl userControl = new UserControl();

            string[] numbers = new string[52] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
            int randomNumber = Utils.RandomBetweenTwoNumbers(0, 51);

            userControl = new Shapes.CoolLetter(numbers[randomNumber].ToString(), Utils.GetRandomColor());

            userControl.Width = 320;
            userControl.Height = 260;

            if (Orientation == PageOrientation.Landscape || Orientation == PageOrientation.LandscapeLeft || Orientation == PageOrientation.LandscapeRight)
            {
                Canvas.SetLeft(userControl, Utils.RandomBetweenTwoNumbers(0, Convert.ToInt32(550)));
                Canvas.SetTop(userControl, Utils.RandomBetweenTwoNumbers(0, Convert.ToInt32(250)));
            }
            else
            {
                Canvas.SetLeft(userControl, Utils.RandomBetweenTwoNumbers(0, Convert.ToInt32(260)));
                Canvas.SetTop(userControl, Utils.RandomBetweenTwoNumbers(0, Convert.ToInt32(550)));
            }

            figuresCanvas.Children.Add(userControl);
        }

        private void addRandomNumber()
        {
            UserControl userControl = new UserControl();

            int[] numbers = new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            int randomNumber = Utils.RandomBetweenTwoNumbers(0, 9);

            userControl = new Shapes.CoolLetter(numbers[randomNumber].ToString(), Utils.GetRandomColor());

            userControl.Width = 320;
            userControl.Height = 260;

            if (Orientation == PageOrientation.Landscape || Orientation == PageOrientation.LandscapeLeft || Orientation == PageOrientation.LandscapeRight)
            {
                Canvas.SetLeft(userControl, Utils.RandomBetweenTwoNumbers(0, Convert.ToInt32(600)));
                Canvas.SetTop(userControl, Utils.RandomBetweenTwoNumbers(0, Convert.ToInt32(250)));
            }
            else
            {
                Canvas.SetLeft(userControl, Utils.RandomBetweenTwoNumbers(0, Convert.ToInt32(300)));
                Canvas.SetTop(userControl, Utils.RandomBetweenTwoNumbers(0, Convert.ToInt32(550)));
            }

            figuresCanvas.Children.Add(userControl);
        }

        private void addRandomSymbol()
        {
            UserControl userControl = new UserControl();

            int randomNumber = Utils.RandomBetweenTwoNumbers(1, 7);
            Color randomColor = Utils.GetRandomColor();

            switch (randomNumber)
            {
                case 1:
                    userControl = new Shapes.CoolCircle(Utils.GetGradientBrush(randomColor));
                    break;
                case 2:
                    userControl = new Shapes.CoolHeart(Utils.GetGradientBrush(randomColor));
                    break;
                case 3:
                    userControl = new Shapes.CoolHexagon(Utils.GetGradientBrush(randomColor));
                    break;
                case 4:
                    userControl = new Shapes.CoolRectangle(Utils.GetGradientBrush(randomColor));
                    break;
                case 5:
                    userControl = new Shapes.CoolSquare(Utils.GetGradientBrush(randomColor));
                    break;
                case 6:
                    userControl = new Shapes.CoolStar(Utils.GetGradientBrush(randomColor));
                    break;
                case 7:
                    userControl = new Shapes.CoolTrapezoid(Utils.GetGradientBrush(randomColor));
                    break;
                case 8:
                    userControl = new Shapes.CoolTriangle(Utils.GetGradientBrush(randomColor));
                    break;
            }

            userControl.Width = 100;  
            userControl.Height = 100;

            if (Orientation == PageOrientation.Landscape || Orientation == PageOrientation.LandscapeLeft || Orientation == PageOrientation.LandscapeRight)
            {
                Canvas.SetLeft(userControl, Utils.RandomBetweenTwoNumbers(0, Convert.ToInt32(700)));
                Canvas.SetTop(userControl, Utils.RandomBetweenTwoNumbers(0, Convert.ToInt32(300)));
            }
            else
            {
                Canvas.SetLeft(userControl, Utils.RandomBetweenTwoNumbers(0, Convert.ToInt32(200)));
                Canvas.SetTop(userControl, Utils.RandomBetweenTwoNumbers(0, Convert.ToInt32(600)));
            }

            figuresCanvas.Children.Add(userControl);
        }

        private void playRandomSound()
        {
            if (sound)
            {
                int randomSound = Utils.RandomBetweenTwoNumbers(1, 7);
                media.Source = new Uri("Audio/" + randomSound + ".wav", UriKind.Relative);
                media.Play();
            }
        }

        private void Sound_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (txtSound.Text == "Music: On")
            {
                txtSound.Text = "Music: Off";
                sound = false;

                Uri uri = new Uri("sound_off.png", UriKind.Relative);
                ImageSource imgSource = new BitmapImage(uri);
                imgSound.Source = imgSource;
            }
            else if (txtSound.Text == "Music: Off")
            {
                txtSound.Text = "Music: On";
                sound = true;

                Uri uri = new Uri("sound_on.png", UriKind.Relative);
                ImageSource imgSource = new BitmapImage(uri);
                imgSound.Source = imgSource;
            }
        }

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            panelStart.Visibility = Visibility.Collapsed;
            figuresCanvas.MouseLeftButtonDown +=new MouseButtonEventHandler(figuresCanvas_MouseLeftButtonDown);

            if (sound)
            {
                if (Microsoft.Xna.Framework.Media.MediaPlayer.GameHasControl)
                {
                    media.Source = new Uri("Audio/BabySmash.wav", UriKind.Relative);
                    media.Play();
                }
                else
                {
                    MessageBoxResult result = MessageBox.Show("You have choosen in settings to use background music, but you are already listening to music. Accept if you want the game to control the music.", "Confirm game's control of music", MessageBoxButton.OKCancel);

                    if (result == MessageBoxResult.OK)
                    {
                        media.Source = new Uri("Audio/BabySmash.wav", UriKind.Relative);
                        media.Play();
                    }
                    else
                    {
                        sound = false;
                    }
                }
            }
        }
    }
}